﻿using DataStructure;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace DataStructure.Test
{


    /// <summary>
    ///This is a test class for CArrayTest and is intended
    ///to contain all CArrayTest Unit Tests
    ///</summary>
    [TestClass()]
    public class CArrayTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for BubbleSort
        ///</summary>
        [TestMethod()]
        public void BubbleSortTest()
        {
            int length = 10;
            CArray target = new CArray(length);
            Random rnd = new Random(100);
            for (int i = 0; i < length; i++)
                target.Insert((int)(rnd.NextDouble() * 100));

            Console.WriteLine("Before Sorting:");
            target.DisplayElements();

            Console.WriteLine();
            Console.WriteLine("During Sorting");
            target.BubbleSort();

            Console.WriteLine();
            Console.WriteLine("After Sorting:");
            target.DisplayElements();
            for (int i = 0; i < target.Arr.Length - 1; i++)
            {
                Assert.IsTrue(target.Arr[i] <= target.Arr[i + 1]);
            }
        }

        /// <summary>
        ///A test for SelectionSort
        ///</summary>
        [TestMethod()]
        public void SelectionSortTest()
        {
            int length = 10;
            CArray target = new CArray(length);
            Random rnd = new Random(100);
            for (int i = 0; i < length; i++)
                target.Insert((int)(rnd.NextDouble() * 100));

            Console.WriteLine("Before Sorting:");
            target.DisplayElements();

            Console.WriteLine();
            Console.WriteLine("During Sorting");
            target.SelectionSort();

            Console.WriteLine();
            Console.WriteLine("After Sorting:");
            target.DisplayElements();
            for (int i = 0; i < target.Arr.Length - 1; i++)
            {
                Assert.IsTrue(target.Arr[i] <= target.Arr[i + 1]);
            }
        }

        /// <summary>
        ///A test for InsertionSort
        ///</summary>
        [TestMethod()]
        public void InsertionSortTest()
        {
            int length = 10;
            CArray target = new CArray(length);
            Random rnd = new Random(100);
            for (int i = 0; i < length; i++)
                target.Insert((int)(rnd.NextDouble() * 100));

            Console.WriteLine("Before Sorting:");
            target.DisplayElements();

            Console.WriteLine();
            Console.WriteLine("During Sorting");
            target.InsertionSort();

            Console.WriteLine();
            Console.WriteLine("After Sorting:");
            target.DisplayElements();
            for (int i = 0; i < target.Arr.Length - 1; i++)
            {
                Assert.IsTrue(target.Arr[i] <= target.Arr[i + 1]);
            }
        }

        [TestMethod]
        public void PerfComparison()
        {
            Console.WriteLine("test result:{0}", 10);
            Perf(10);

            Console.WriteLine("test result:{0}", 100);
            Perf(100);

            Console.WriteLine("test result:{0}", 1000);
            Perf(1000);

            Console.WriteLine("test result:{0}", 10000);
            // Perf(10000);
        }


        private void Perf(int numItems)
        {
            Timing sortTime = new Timing();
            Random rnd = new Random(numItems);
            CArray theArray = new CArray(numItems);
            for (int i = 0; i < numItems; i++)
                theArray.Insert((int)(rnd.NextDouble() * 10000));

            sortTime.StartTime();

            theArray.SelectionSort();
            sortTime.StopTime();

            Console.WriteLine("Time for Selection sort: " + sortTime.Result().TotalMilliseconds);
            theArray.Clear();
            for (int i = 0; i < numItems; i++)
                theArray.Insert((int)(rnd.NextDouble() * 100));
            sortTime.StartTime();
            theArray.BubbleSort();
            sortTime.StopTime();
            Console.WriteLine("Time for Bubble sort: " + sortTime.Result().TotalMilliseconds);
            theArray.Clear();
            for (int i = 0; i < numItems; i++)
                theArray.Insert((int)(rnd.NextDouble() * 100));
            sortTime.StartTime();
            theArray.InsertionSort();
            sortTime.StopTime();
            Console.WriteLine("Time for Selection sort: " + sortTime.Result().TotalMilliseconds);
        }

        /// <summary>
        ///A test for ShellSort
        ///</summary>
        [TestMethod()]
        public void ShellSortTest()
        {
            int length = 10;
            CArray target = new CArray(length) { displayElements = true };
            Random rnd = new Random(100);
            for (int i = 0; i < length; i++)
                target.Insert((int)(rnd.NextDouble() * 100));

            Console.WriteLine("Before Sorting:");
            target.DisplayElements();

            Console.WriteLine();
            Console.WriteLine("During Sorting");
            target.ShellSort();

            Console.WriteLine();
            Console.WriteLine("After Sorting:");
            target.DisplayElements();
            for (int i = 0; i < target.Arr.Length - 1; i++)
            {
                Assert.IsTrue(target.Arr[i] <= target.Arr[i + 1]);
            }
        }

        /// <summary>
        ///A test for MergeSort
        ///</summary>
        [TestMethod()]
        public void MergeSortTest()
        {

            int length = 10;
            CArray target = new CArray(length) { displayElements = true };
            Random rnd = new Random(100);
            for (int i = 0; i < length; i++)
                target.Insert((int)(rnd.NextDouble() * 100));

            Console.WriteLine("Before Sorting:");
            target.DisplayElements();

            Console.WriteLine();
            Console.WriteLine("During Sorting");
            target.MergeSort();

            Console.WriteLine();
            Console.WriteLine("After Sorting:");
            target.DisplayElements();
            for (int i = 0; i < target.Arr.Length - 1; i++)
            {
                Assert.IsTrue(target.Arr[i] <= target.Arr[i + 1]);
            }
        }

        /// <summary>
        ///A test for QuickSort
        ///</summary>
        [TestMethod()]
        public void QuickSortTest()
        {
            int length = 10;
            CArray target = new CArray(length) { displayElements = true };

           // target.Arr = new int[] { 87, 91, 65, 72, 84, 99, 89};
            Random rnd = new Random(100);
            for (int i = 0; i < length; i++)
                target.Insert((int)(rnd.NextDouble() * 100));

            Console.WriteLine("Before Sorting:");
            target.DisplayElements();

            Console.WriteLine();
            Console.WriteLine("During Sorting");
            target.QuickSort();

            Console.WriteLine();
            Console.WriteLine("After Sorting:");
            target.DisplayElements();
            for (int i = 0; i < target.Arr.Length - 1; i++)
            {
                Assert.IsTrue(target.Arr[i] <= target.Arr[i + 1]);
            }
        }
    }
}
